
import datetime

from google.appengine.ext import db

#App imports
import models
import budget
import appuser

class CategoryDao():
  
  def GetCatKeyName(self, pCatName):
    userEmail = appuser.GetUserEmail();
    catKeyName = userEmail.lower() + pCatName.lower()
    return catKeyName
  
  def doCheckIfCategoryExists(self, pCatName):
    """This method checks if there is category exists return true or false."""
    catKeyName = self.GetCatKeyName(pCatName)
    category = models.Category.get_by_key_name(catKeyName)  
    if not category:
      return False
    else:
      return True
    
  def doAddCategory(self, pUser, pCatName):
    """This method checks if there is category exists if does then it returns that record
      else it adds a new record and returns it."""
    catKeyName = self.GetCatKeyName(pCatName)
    category = models.Category(key_name=catKeyName, CategoryName=pCatName.upper(), User=pUser.key(), Active=True, CreateId=pUser.key(), ModId=pUser.key())
    category.put()
    return category

  def GetCategory(self, pCatName):
    """This method gets the category record for a category name."""
    catKeyName = self.GetCatKeyName(pCatName)
    category = models.Category.get_by_key_name(catKeyName)  
    return category
